import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/common_lib/routers/route.dart';
import 'package:flutter_massage/generated/assets.dart';
import 'package:flutter_massage/service/index.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class SystemSplashPage extends StatefulWidget {
  const SystemSplashPage({Key? key}) : super(key: key);

  @override
  State<SystemSplashPage> createState() => _SystemSplashPageState();
}

class _SystemSplashPageState extends State<SystemSplashPage> {
  @override
  void initState() {
    super.initState();

    init();
  }

  void init() {
    TextSpan agreement = TextSpan(
      text: '《用户协议》',
      style: const TextStyle(color: AppColors.primaryGreen),
      recognizer: TapGestureRecognizer()..onTap = CommonRoute.openProtocolPage,
    );

    TextSpan privacy = TextSpan(
      text: '《隐私政策》',
      style: const TextStyle(color: AppColors.primaryGreen),
      recognizer: TapGestureRecognizer()..onTap = CommonRoute.openPrivacyPage,
    );

    Widget description = Text.rich(
      TextSpan(
        text: '亲爱的用户，感谢您信任并使用平台！我们依据相关法律法规制定了',
        children: [
          agreement,
          const TextSpan(text: '和'),
          privacy,
          const TextSpan(text: '，请您仔细阅读并充分理解相关条款，点击“同意”开始接受我们的服务。'),
        ],
      ),
      style: const TextStyle(color: AppColors.textLevelOne, fontSize: 13, height: 1.5),
    );

    SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
      CommonDialog.twoButton(
        title: '温馨提示',
        descriptionWidget: description,
        cancelTitle: '不同意',
        confirmTitle: '同意',
        onCancel: () {
          CommonDialog.twoButton(
            description: '本产品需要同意后才能使用。',
            cancelTitle: '退出',
            confirmTitle: '同意',
            onConfirm: openMainPage,
            onCancel: CommonOperate.exitApp,
          );
        },
        onConfirm: openMainPage,
      );
    });
  }

  void openMainPage() async {
    Storage().setBool(StorageKey.allowUseApp, true);
    // SettingBean bean = await XydUserService.getCacheMineInfoBean(refresh: true);
    // if (bean.prelogin.equals('1')) {
    //   CommonRoute.openLogin();
    // } else {
    CommonRoute.openMain();
    // }
    await XydUserService.getIdfa();
  }

  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: const Alignment(0, -0.3),
      child: ImageWidget.asset(Assets.imageLogoDark, width: 118.w, height: 118.w),
    );
  }
}
